package com.zhuss.order.processor;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.order.order.ConfirmTakeBo;
import com.zhuss.bean.vo.order.order.ConfirmTakeVo;
import com.zhuss.bean.vo.order.order.DetailOrderItemVo;
import com.zhuss.bean.vo.order.order.DetailOrderVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.order.db.dao.ext.OrderDao;
import com.zhuss.order.db.dao.ext.OrderItemDao;
import com.zhuss.order.db.dao.ext.PrepayDao;
import com.zhuss.util.JsonUtil;

/**
 * 确认上车 processor
 * @Author: zhushanshuo
 * @Date: 2021/01/15 20:18
 */
@Component("takeConfirm")
public class TakeConfirm extends AbstractProcessor<ConfirmTakeVo, ConfirmTakeBo> implements Processor<ConfirmTakeVo, ConfirmTakeBo> {

    
    @Autowired
    private OrderDao orderDao;
    
    @Autowired
    private OrderRead orderRead;
    
    @Autowired
    private OrderItemDao orderItemDao;
    
    @Autowired
    private PrepayDao prepayDao;
    
    /**
     * 校验
     */
    @Override
    public void check(ConfirmTakeBo param) throws DzcxException {
    	Long orderId = param.getOrderId();
    	Long trainId = param.getTrainId();
    	if (orderId == null || orderId <= 0 || trainId == null || trainId <= 0) {
    		throw new DzcxException(ExceptionCode.订单ID不存在);
    	}
    	
    	DetailOrderVo order = orderRead.detail(orderId);
    	
    	DetailOrderItemVo orderItem = orderItemDao.getTrainByTake(orderId, trainId, OrderStatus.支付成功.key());
    	
    	if (order == null) {
    		throw new DzcxException(ExceptionCode.确认上车的车票不存在);
    	}
    	
    	if (orderItem == null) {
    		throw new DzcxException(ExceptionCode.车次不存在或未支付);
    	}
    	
    	Integer status = order.getStatus();
    	Integer ticketStatus = orderItem.getStatus();
    	if (!OrderStatus.支付成功.sameKey(status) || !OrderStatus.支付成功.sameKey(ticketStatus)) {
    		throw new DzcxException(ExceptionCode.未支付不能确认上车);
    	}
    	
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(new Date());
    	// TODO
    	cal.add(Calendar.MINUTE, 10 * 6 * 24 * 10);
    	if (cal.getTime().getTime() < orderItem.getOnSiteTime().getTime()) {
    		throw new DzcxException(ExceptionCode.未到上车时间);
    	}
    	
    	param.setOrder(order);
    	param.setOrderItem(orderItem);
    	
    }
    
    /**
     * 操作方法
     * @throws DzcxException 
     */
    @Override
    public ConfirmTakeVo process(ConfirmTakeBo param) throws DzcxException {
    	
    	DetailOrderVo order = param.getOrder();
    	Long trainId = param.getOrderItem().getTrainId();
    	
    	Date now = new Date();
    	
    	orderItemDao.refreshStatusByTrain(order.getOrderId(), trainId, OrderStatus.已确认上车.key(), param.getConcurrentUserName(), param.getConcurrentUserId(), now);
    	ConfirmTakeVo result = new ConfirmTakeVo();
    	
        return result;
    }
    
    /**
     * 操作日志
     */
	@Override
	public ConfirmTakeVo log(ConfirmTakeBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.订单.key());
    	record.setAction(LogActionEnum.确认上车.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getOrderId() + ": trainId=" + param.getOrderItem().getTrainId());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
